import config from "./utils/config";
// import { getUserInfo } from './utils/utils';
const QQMapWX = require("./libs/qqmap-wx-jssdk.min.js");
const qqmapsdk = new QQMapWX({ key: config.QQMapKey });
import { getUserInfo } from "@/api/index";
export default {
  computed: {
    userInfo() {
      let userInfo = uni.getStorageSync("userInfo");
      if (userInfo) {
        return userInfo;
      }
      return "";
    },
    wid() {
      let userInfo = uni.getStorageSync("userInfo");
      if (userInfo) {
        return userInfo.id;
      }
      return "";
    },
  },
  methods: {
    toTree(node) {
      node.forEach(function (it) {
        delete it.children;
      });
      // 定义map/
      var map = {};
      // 这里可以重构数据类型，放回字段值
      node.forEach(function (it) {
        map[it.id] = it;
      });

      // 定义返回集合
      var val = [];
      node.forEach(function (it) {
        var parent = map[it.pid];
        if (parent) {
          // 有数据说明不是顶级节点，将数据放到该 children 子节点下
          (parent.children || (parent.children = [])).push(it);
        } else {
          // 没有数据说明是顶级节点放到val中
          val.push(it);
        }
      });
      return val;
    },
    changeRemark(val) {
      this.remark = val;
    },
    async getUserInfo() {
      let res = await getUserInfo({ wid: this.wid });
      this.$store.commit("set_userInfo", res);
    },
    deepClone(obj) {
      let objClone = Array.isArray(obj) ? [] : {};
      if (obj && typeof obj === "object") {
        for (let key in obj) {
          if (obj.hasOwnProperty(key)) {
            if (obj[key] && typeof obj[key] === "object") {
              objClone[key] = deepClone(obj[key]);
            } else {
              objClone[key] = obj[key];
            }
          }
        }
      }
      return objClone;
    },
    popClose() {
      this.popShow = false;
    },
    popOpen() {
      this.popShow = true;
    },
    // 获取用户当前坐标
    async getPosition() {
      // return new Promise((resolve, reject) => {
      //   qqmapsdk.reverseGeocoder({
      //     success: (res) => {
      //       console.log(res);
      //       const { city } = res.result.ad_info;
      //       const { lat, lng } = res.result.location;
      //       this.setData({
      //         latitude: lat,
      //         longitude: lng,
      //         currentCity: city,
      //       });
      //       console.log(lat, lng, city);
      //       wx.setStorageSync("latitude", lat);
      //       wx.setStorageSync("longitude", lng);
      //       resolve("ok");
      //     },
      //     fail: (err) => {
      //       console.log(err);
      //       wx.showModal({
      //         title: "提示",
      //         content:
      //           "1.请将【位置消息】设为【使用小程序时允许】；2.手机设置-微信-位置-使用APP期间",
      //         showCancel: false,
      //         success: (res) => {
      //           if (res.confirm) {
      //             this.openLocationSetting();
      //           }
      //         },
      //       });
      //       reject(err);
      //     },
      //   });
      // });
      return new Promise((resolve, reject) => {
        wx.getLocation({
          type: "wgs84",
          success(res) {
            console.log(res, 555);
            const latitude = res.latitude;
            const longitude = res.longitude;
            const speed = res.speed;
            const accuracy = res.accuracy;
            wx.setStorageSync("latitude", latitude);
            wx.setStorageSync("longitude", longitude);
            resolve(res);
          },
          fail(err) {
            reject(err);
          },
        });
      });
    },
    // 打开设置
    openLocationSetting() {
      console.log(3333);
      wx.openSetting({
        success: (res) => {
          if (!res.authSetting["scope.userLocation"]) {
            wx.authorize({
              scope: "scope.userLocation",
              success: () => {
                this.getPosition();
              },
            });
          } else {
            this.getPosition();
          }
        },
      });
    },

    jump(url, cb = null) {
      cb && cb();
      wx.navigateTo({
        url,
      });
    },

    getGPS(latitude = "", longitude = "") {
      if (latitude && longitude) {
        wx.openLocation({
          latitude: Number(latitude),
          longitude: Number(longitude),
          scale: 18,
        });
      } else {
        wx.showToast({
          title: "请更新客户坐标",
          icon: "none",
        });
      }
    },
    // 计算两个经纬度
    getDistance(lat1, lon1, lat2, lon2) {
      if (lat1 && lon1 && lat2 && lon2) {
        // 将角度转换为弧度
        function toRadians(degrees) {
          return (degrees * Math.PI) / 180;
        }

        // 使用 Haversine 公式计算距离
        const R = 6371; // 地球半径，单位为千米
        const dLat = toRadians(lat2 - lat1);
        const dLon = toRadians(lon2 - lon1);

        const a =
          Math.sin(dLat / 2) * Math.sin(dLat / 2) +
          Math.cos(toRadians(lat1)) *
            Math.cos(toRadians(lat2)) *
            Math.sin(dLon / 2) *
            Math.sin(dLon / 2);

        const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        const distance = R * c; // 距离，单位为千米
        return distance.toFixed(2) + " km";
      } else {
        return "";
      }
    },

        // 计算两个经纬度
        getDistance2(lat1, lon1, lat2, lon2) {
          if (lat1 && lon1 && lat2 && lon2) {
            // 将角度转换为弧度
            function toRadians(degrees) {
              return (degrees * Math.PI) / 180;
            }
    
            // 使用 Haversine 公式计算距离
            const R = 6371; // 地球半径，单位为千米
            const dLat = toRadians(lat2 - lat1);
            const dLon = toRadians(lon2 - lon1);
    
            const a =
              Math.sin(dLat / 2) * Math.sin(dLat / 2) +
              Math.cos(toRadians(lat1)) *
                Math.cos(toRadians(lat2)) *
                Math.sin(dLon / 2) *
                Math.sin(dLon / 2);
    
            const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    
            const distance = R * c; // 距离，单位为千米
            return distance;
          } else {
            return 999999999999999;
          }
        },
  },
};
